package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.SysRoleDao;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysRoleMenuService;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 角色
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    //角色列表
	@Override
	public PageUtils getList(Map<String, Object> params) {
		String name = null;
		if (null != params.get("name") && !"".equals(params.get("name"))) {
			name = String.valueOf(params.get("name"));
		}

		QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
		IPage<SysRoleEntity> page = this.page(new Query<SysRoleEntity>().getPage(params),
				wrapper.like(StringUtils.isNotBlank(name),"role_name", name));

		List<SysRoleEntity> records = page.getRecords();
		for (SysRoleEntity role : records) {
			if ("1".equals(role.getState())) {
				role.setStateBool(true);
			} else {
				role.setStateBool(false);
			}
		}
		return new PageUtils(page);
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role) {
        role.setCreateTime(new Date());
		role.setState("1");
        this.save(role);

        //检查权限是否越权
        //checkPrems(role);

        //保存角色与菜单关系
        //sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
		role.setCreateTime(new Date());
		if (role.isStateBool()) {
			role.setState("1");
		} else {
			role.setState("0");
		}
        this.updateById(role);

        //检查权限是否越权
        //checkPrems(role);

        //更新角色与菜单关系
        //sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Integer roleId) {
        //删除角色
        this.removeById(roleId);

        //删除角色与菜单关联
        //sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleId);
    }


    @Override
	public List<Integer> queryRoleIdList(Integer createUserId) {
		return baseMapper.queryRoleIdList(createUserId);
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(SysRoleEntity role){
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
		if(role.getCreateUserId() == Constant.SUPER_ADMIN){
			return ;
		}
		
		//查询用户所拥有的菜单列表
		List<Integer> menuIdList = sysUserDao.queryAllMenuId(role.getCreateUserId());
		
		//判断是否越权
		if(!menuIdList.containsAll(role.getMenuIdList())){
			throw new RRException("新增角色的权限，已超出你的权限范围");
		}
	}
}
